package Matrices;

/**
  *  4g3nt
  *  Copyright (C) 2008  Carlos Molinero y César Andrés
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *    You should have received a copy of the GNU General Public License
  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
public class Permutaciones {
    private int[] _valores;
    public Permutaciones(int[] valores)
    {
        _valores= valores;
    }
    public void imprimir()
    {
        for(int i =0 ; i < _valores.length; i++)
            System.out.print(_valores[i]+" ");
        System.out.println();
    }
    public boolean cambio()
    {
        for (int i = _valores.length-1; i >=0; i--)
            for (int j = i-1; j >=0; j--)
        {
            if (_valores[i]>_valores[j])
                {
                cambia(i,j);
                return true;
                }
        }
        return false;
    }
    private void cambia(int ultimo, int primero)
    {
        int var_ultimo = _valores[ultimo];
        for (int i = ultimo; i > primero; i--)
            _valores[i] = _valores[i-1];
        _valores[primero]= var_ultimo;
        ordena(primero+1);
    }
    public void ordena(int a)
    {
         for (int i = a; i < _valores.length; i++)
            for (int j = i; j < _valores.length; j++)
                    if (_valores[i] > _valores[j])
                {
                    int aux = _valores[i];
                    _valores[i] = _valores[j];
                    _valores[j] = aux;
                }
        
        
    }
} 
